import { AssetManager, Canvas, Node, Prefab, director, instantiate, resources } from "cc";
import BDialog from "./BDialog";

/*****************************
 *@file: BDialogBuilder
 *@author: 陈吕唐
 *@desc: 建造者模式创建弹窗
 *@date: 2024-02-27	10:34
 *****************************/
export default class BDialogBuilder {
    /****************************************************************************************************************
     *Readonly 常量
     ****************************************************************************************************************/
    /****************************************************************************************************************
     *Property 定义变量
     ****************************************************************************************************************/
    /**
     * 弹窗父节点
     */
    private dialogParent: Node = undefined!;

    /**
     * 弹窗层级
     */
    private siblingIndex: number = 0;

    /**
     * 弹窗参数
     */
    private _param: any = undefined!;


    /**
     * 弹窗预制体路径
     */
    private _prefabPath: string = "";


    /**
     * 加载进度回调
     */
    private onProgress: (finish: number, total: number, item: AssetManager.RequestItem) => void = undefined!;
    /****************************************************************************************************************
     * Lifecycle 生命周期相关方法
     ****************************************************************************************************************/
    /****************************************************************************************************************
     * Public 公有方法
     ****************************************************************************************************************/
    public build(): Promise<BDialog> {
        let promise: Promise<BDialog> = new Promise((resolve, reject) => {
            resources.load(this.prefabPath, this.onProgress, (err, prefab: Prefab) => {

                if (err) {
                    reject(err);
                    return;
                }

                let node: Node = instantiate(prefab);

                if (!node) {
                    reject("instantiate prefab fail");
                    return;
                }

                let dialog = node.getComponent(BDialog);
                dialog.param = this.param;

                if (!dialog) {
                    reject("can not find BDialog component");
                    return;
                }

                let parentNode: Node = this.dialogParent;

                if (!parentNode) {
                    parentNode = director.getScene()?.getComponentInChildren(Canvas)?.node;
                }

                if (!parentNode) {
                    reject("弹窗挂载父节点为空");
                    return;
                }

                parentNode.addChild(node);
                //TODO: 弹窗层级
                // node.setSiblingIndex(this.siblingIndex);
                resolve(dialog);
            });
        })
        return promise;
    }
    /****************************************************************************************************************
     * private 私有方法
     ****************************************************************************************************************/
    /****************************************************************************************************************
     * Property Get && Set 属性的 get && set 函数
     ****************************************************************************************************************/
    public get prefabPath(): string {
        return this._prefabPath;
    }
    public set prefabPath(value: string) {
        this._prefabPath = value;
    }

    public get param(): any {
        return this._param;
    }
    public set param(value: any) {
        this._param = value;
    }
}